// pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: ['/img/poster-1.jpg', '/img/poster-2.jpg', '/img/poster-3.jpg'],
    interval: 5000,
    gridIcon:['/img/calendar.png','/img/music.png','/img/RankingList.png','/img/question.png','/img/forward.png'],
    sms: '',
    show: false,
    actions: [
      {
        name: '下载',
      },
      {
        name: '添加到播放列表',
      },
    ],
    downloadIndex: 0,
    music: app.globalData.music,
    songs: app.globalData.songs,
    playControls: app.globalData.playControls,
    
  },
  // 上一首
  prev() {
    app.prevAudio();
    setTimeout(() => {
      this.upDataMusic()
      app.globalData.audioCtx.play();
    }, 500)
  },
  // 下一首
  next() {
    app.nextAudio();
    setTimeout(() => {
      this.upDataMusic()
      app.globalData.audioCtx.play();
    }, 500)
  },
  // 播放控制
  playControls() {
    app.playControls();
    this.setData({
      playControls: app.globalData.playControls
    })
  },
  // 数据更新
  upDataMusic() {
    this.setData({
      music: app.globalData.music,
      songs: app.globalData.songs,
      playControls: app.globalData.playControls,
    })
  },
  // 展示弹出层
  showPopup() {
    this.setData({
      show: true
    });
  },
  // 关闭弹出层
  onClose() {
    this.setData({
      show: false
    });
  },
  // 保存当前行的index
  saveIndex(event) {
    this.setData({
      downloadIndex: event.currentTarget.dataset.index
    })
  },
  onSelect(event) {
    console.log(event);
    if(event.detail.name == '下载'){
      this.downloadMusic();
    }
    if(event.detail.name == '添加到播放列表'){
      this.addMusicList();
    }
  },
  // 添加到播放列表
  addMusicList() {
    console.log('addMusicList');
    let index = this.data.downloadIndex;
    let id = this.data.songs[index].id;
    // this.data.musicList.push(id);
    app.globalData.musicList.push(id)
    console.log("addMusicList",app.globalData.musicList);
  },
  async searchMusic() {
    let data = await app.getMusic(this.data.sms).then((value) => {
      console.log("搜索请求返回的内容", value.result);
      return value;
    })
    this.setData({
      ['songs']: data.result.songs
    })
  },
  // 发起下载音乐请求
  async downloadMusic() {
    console.log('downloadMusic');
    let index = this.data.downloadIndex
    let id = this.data.songs[index].id;
    let fileUrl = await app.getMusicSrc(id).then((value) => {
      return value.data[0].url
    })
    console.log(fileUrl);

    wx.downloadFile({
      url: fileUrl,
      success(res) {
        console.log(res);
        if (res.statusCode === 200) {
          console.log(res.tempFilePath);
        }
      },
      fail(error) {
        console.log('fail');
      }
    })
  },
  // 获取要播放音乐的数据
  onPlayClick(event) {
    // 获取当前列的index
    console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    if (this.data.music.id !== this.data.songs[index].id) {
        app.globalData.playControls = 'stop'
    }
    this.setData({
      ['music.id']: this.data.songs[index].id,
      ['music.name']: this.data.songs[index].name,
      ['music.poster']: this.data.songs[index].artists[0].img1v1Url,
      ['music.author']: this.data.songs[index].artists[0].name
    });
    app.setMusicSrc(this.data.music.id);
    // 由于函数有异步操作所以使用setTimeout延时触发将playControls()函数改造成异步的并在最后触发
    setTimeout(() => {
      app.globalData.audioCtx.setSrc(app.globalData.music.src);
      this.playControls()
    }, 500)
  },
  myAbout(){
    wx.switchTab({
      url: '/pages/about/about'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.setData();
    // 创建音频组件
    this.audioCtx = wx.createAudioContext('myAudio')
    // 保存到全局
    app.globalData.audioCtx = this.audioCtx;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    setTimeout(() => {
      this.upDataMusic();
    }, 500)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})